<template>
  <el-dialog
    title="分配角色"
    :visible="showRoles"
    width="50%"
    @close="onClose"
  >
    <div>当前的用户: {{ form.username }}</div>
    <div>当前的角色: 超级管理员</div>
    <div>分配新的角色:
      <el-select v-model.trim="value" placeholder="请选择">
        <el-option
          v-for="(item,index) in options"
          :key="index"
          :label="item"
          :value="item"
        />
      </el-select>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="onClose">取 消</el-button>
      <el-button type="primary" @click="onSave">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getRolesList } from '@/api/role.js'
import { RolesUserInfo } from '@/api/user.js'
export default {
  props: {
    showRoles: {
      type: Boolean,
      required: true
    },
    form: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      options: [],
      value: ''
    }
  },

  created() {
    this.loadRolesList()
  },

  methods: {
    async loadRolesList() {
      const res = await getRolesList()
      this.options = res.data.map(item => item.roleName)
    },
    // 关闭弹层
    onClose() {
      this.$emit('ChangeShow', false)
      this.value = ''
    },
    // 分配角色
    async onSave() {
      await RolesUserInfo(this.form)
      if (this.value === '') {
        this.$message.error('亲，请选择角色呦')
      } else {
        this.$emit('ChangeShow', false)
        this.$message.success('亲，分配成功了呦')
      }
      this.value = ''
    }
  }
}
</script>

<style scoped lang='scss'>
.el-dialog__body{
  div{
    margin: 14px 0;
    &:last-child{
      margin: 0;
    }
  }
}
</style>
